package com.trigsoft.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

public class BitmapUtil
{
	private static int sIconWidth = -1;
	private static int sIconHeight = -1;
	private static int sIconTextureWidth = -1;
	private static int sIconTextureHeight = -1;

	private static final Rect sOldBounds = new Rect();
	private static final Canvas sCanvas = new Canvas();

	static
	{
		sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, Paint.FILTER_BITMAP_FLAG));
	}

	/**
	 * Returns a bitmap suitable for the all apps view.  The bitmap will be a power
	 * of two sized ARGB_8888 bitmap that can be used as a gl texture.
	 */
	public static Bitmap createIconBitmap(Drawable icon, Context context)
	{
		synchronized (sCanvas)
		{ // we share the statics :-(
			if (sIconWidth == -1)
			{
				initStatics(context);
			}

			int width = sIconWidth;
			int height = sIconHeight;

			if (icon instanceof PaintDrawable)
			{
				PaintDrawable painter = (PaintDrawable) icon;
				painter.setIntrinsicWidth(width);
				painter.setIntrinsicHeight(height);
			} else if (icon instanceof BitmapDrawable)
			{
				// Ensure the bitmap has a density.
				BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
				Bitmap bitmap = bitmapDrawable.getBitmap();
				if (bitmap.getDensity() == Bitmap.DENSITY_NONE)
				{
					bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
				}
			}
			int sourceWidth = icon.getIntrinsicWidth();
			int sourceHeight = icon.getIntrinsicHeight();

			if (sourceWidth > 0 && sourceWidth > 0)
			{
				// There are intrinsic sizes.
				if (width < sourceWidth || height < sourceHeight)
				{
					// It's too big, scale it down.
					final float ratio = (float) sourceWidth / sourceHeight;
					if (sourceWidth > sourceHeight)
					{
						height = (int) (width / ratio);
					} else if (sourceHeight > sourceWidth)
					{
						width = (int) (height * ratio);
					}
				} else if (sourceWidth < width && sourceHeight < height)
				{
					// It's small, use the size they gave us.
					width = sourceWidth;
					height = sourceHeight;
				}
			}

			// no intrinsic size --> use default size
			int textureWidth = sIconTextureWidth;
			int textureHeight = sIconTextureHeight;

			final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
			final Canvas canvas = sCanvas;
			canvas.setBitmap(bitmap);

			final int left = (textureWidth - width) / 2;
			final int top = (textureHeight - height) / 2;

			sOldBounds.set(icon.getBounds());
			icon.setBounds(left, top, left + width, top + height);
			icon.draw(canvas);
			icon.setBounds(sOldBounds);

			return bitmap;
		}
	}

	private static void initStatics(Context context)
	{
		final Resources resources = context.getResources();
		//final DisplayMetrics metrics = resources.getDisplayMetrics();

		sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
		Log.d("test", Integer.toString(sIconWidth));
		sIconTextureWidth = sIconTextureHeight = sIconWidth + 2;
	}

	public static boolean isPortrait(Context context)
	{
		Display d = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

		return (d.getWidth() < d.getHeight());
	}
}
